
package com.yimixian.app.order;

import com.umeng.analytics.MobclickAgent;
import com.yimixian.app.R;
import com.yimixian.app.SystemFramework;
import com.yimixian.app.activity.BaseNaviFragmentGroupActivity;
import com.yimixian.app.common.Constants;
import com.yimixian.app.common.StatConstants;
import com.yimixian.app.model.ValidatedOrder;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

import java.util.ArrayList;

public class SelectCouponActivity extends BaseNaviFragmentGroupActivity implements
        View.OnClickListener {
    public static final String INTENT_EXTRA_COUPON_LIST = "coupon_list";
    public static final String INTENT_EXTRA_COUPON_SELECT = "coupon_select";
    private ArrayList<ValidatedOrder.Bonus> mList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navibar_simple);
        MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
            StatConstants.CONFIRM_CHOOSE_COUPON);
        mList.clear();
        mList = getIntent().getParcelableArrayListExtra(INTENT_EXTRA_COUPON_LIST);
        setTopNavi(R.drawable.ic_back_selector,
                getString(R.string.confirm_ok), getString(R.string.select_coupon),
                this, this);

    }

    public static Intent buildIntent(Context context, ArrayList<ValidatedOrder.Bonus> list) {
        Intent intent = new Intent(context, SelectCouponActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        intent.putParcelableArrayListExtra(INTENT_EXTRA_COUPON_LIST, list);
        return intent;
    }

    @Override
    protected void initPrimaryFragment() {
        switchPrimaryFragment(0);
    }

    @Override
    protected Class<? extends Fragment> getPrimaryFragmentClass(int fragmentId) {
        return SelectCouponFragment.class;
    }

    @Override
    protected Bundle getPrimaryFragmentArguments(int fragmentId) {
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(INTENT_EXTRA_COUPON_LIST, mList);
        return bundle;
    }

    @Override
    protected int getPrimaryFragmentStubId(int fragmentId) {
        return R.id.content_frame;
    }

    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_bar_right_text:
                if (mCurrentPrimaryFragment instanceof SelectCouponFragment) {
                    int pos = ((SelectCouponFragment) mCurrentPrimaryFragment).getSelectPos();
                    Intent intent = new Intent();
                    intent.putExtra(INTENT_EXTRA_COUPON_SELECT, pos);
                    this.setResult(
                            Constants.REQUESTCODE_SELECT_COUPON,
                            intent);
                    finish();
                }
                break;

            case R.id.title_bar_left_btn:
                onBackPressed();
                break;

            default:
                break;
        }
    }
}
